/*
*	Copyright (c) 2009 Brent Mombourquette
*
*	Permission is hereby granted, free of charge, to any person
*	obtaining a copy of this software and associated documentation
*	files (the "Software"), to deal in the Software without
*	restriction, including without limitation the rights to use,
*	copy, modify, merge, publish, distribute, sublicense, and/or sell
*	copies of the Software, and to permit persons to whom the
*	Software is furnished to do so, subject to the following
*	conditions:
*
*	The above copyright notice and this permission notice shall be
*	included in all copies or substantial portions of the Software.
*
*	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
*	EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
*	OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
*	NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
*	HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
*	WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
*	FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
*	OTHER DEALINGS IN THE SOFTWARE.
*
*	This file contains the functions which oversee the editing of edge annotations and node titles 
*	(sometimes I refer to node titles as node annotations as well)
*/

var annotationEdit = {
	
	/*
	 * Open a window to enter a new annotation for dataNode.
	 * @param{DataNode} dataNode 
	 */
	node: function(dataNode) {
		graphHTMLControl.closeNodeContext();
		// Open the window for entering a new annotation, and pass it the needed variables
		var annWindow = window.openDialog("chrome://BreadCrumbs/content/annotate_node.xul", "Title", "chrome,centerscreen", {oldTitle: dataNode.title, url: dataNode.url, row: dataNode.nodeURLKey} );
		//Check every second to see if annWindow has been closed
		var annTimer = setTimeout(function(){annotationEdit.watchNodeForClose(annWindow, annTimer, dataNode)},1000);
	},
	
	/*
	 * Once annWindow is closed, update the node's database entry. This function is called on a timer.
	 * @param {Window} annWindow
	 * @param {Timer} annTimer
	 * @param {DataNode} node
	 */
	watchNodeForClose: function(annWindow, annTimer, node) {
		//This needs to be checked since the (potentially) new annotation will not be displayed on the tooltip of that node of the graph unless either the graph
		//is updated or its annotation property gets reset.
		if (!annWindow || annWindow.closed) { 
			node.title = database.getTitle(node.nodeURLKey); //Node tooltip updated
			clearTimeout(annTimer); //Stop the timer
		} else { // Continue the timer
			annTimer = setTimeout(function(){annotationEdit.watchNodeForClose(annWindow, annTimer, node)},1000);
		}
	},
	
	/*
	 * Get the SVG edge object corresponding to id and pass it to annotationEdit.edge.
	 * @param {String} id
	 */
	preEdge: function(id) {
		var e = document.getElementById(id);
		annotationEdit.edge(e)
	},
	
	/*
	 * Open a new window to enter a new annotation for edge.
	 * @param {SVG Polyline} edge
	 */
	edge: function(edge) {
		graphHTMLControl.closeEdgeContext();
		var edgeAnnWindow = window.openDialog("chrome://BreadCrumbs/content/annotate_edge.xul", "Edit Edge Annotation", "chrome,centerscreen", {oldAnnotation: edge.annotation, id: edge.idx } );
		//Check every second to see if annotationWindow has been closed
		var edgeAnnTimer = setTimeout(function(){annotationEdit.watchEdgeForClose(edgeAnnWindow, edgeAnnTimer, edge)},1000);
	},
	
	/*
	 * Once edgeAnnWindow is closed, update the edge's database entry. This function is called on a timer.
	 * @param {Window} edgeAnnWindow
	 * @param {Timer} edgeAnnTimer
	 * @param {SVG Polyline} edge
	 */
	watchEdgeForClose: function(edgeAnnWindow, edgeAnnTimer, edge){
		//This needs to be checked since the (potentially) new annotation will not be displayed on the tooltip of that node of the graph unless either the graph
		//is updated or its annotation property gets reset.
		if (!edgeAnnWindow || edgeAnnWindow.closed) { 
			edge.annotation = database.getAnnotation(edge); // Update the annotation for the tooltip to display
			clearTimeout(edgeAnnTimer); //Stop the timer
		} else { // Conitune the timer
			edgeAnnTimer = setTimeout(function(){annotationEdit.watchEdgeForClose(edgeAnnWindow, edgeAnnTimer, edge)},1000);
		}
	}
}